import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '_views/Home'
import { isLogin } from '_utils'

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/home'
},
{
  path: '/home',
  name: '首页',
  component: Home,
  meta: {
    needLogin: false
    // keepAlived: true
  }
},
{
  path: '/cart',
  name: '购物车',
  component: () => import('_views/Cart'),
  meta: {
    needLogin: true
  }
},
{
  path: '/cate',
  name: '分类',
  component: () => import('_views/Cate'),
  meta: {
    needLogin: false
  }
},
{
  path: '/itemList',
  name: '商品列表页',
  component: () => import('_views/ItemList'),
  meta: {
    needLogin: false
    // keepAlived: true
  }
},
{
  path: '/user',
  name: '我的',
  component: () => import('_views/User'),
  meta: {
    needLogin: false
  }
},
{
  path: '/detail/:id',
  name: '详情页',
  component: () => import('_views/Detail'),
  meta: {
    needLogin: false
  }
},
{
  path: '/login',
  name: '登录页',
  component: () => import('_views/Login'),
  meta: {
    needLogin: false
  }
},
{
  path: '/register',
  name: '注册页面',
  component: () => import('_views/Register'),
  meta: {
    needLogin: false
  }
},
{
  path: '/search',
  name: '搜索',
  component: () => import('_views/Search'),
  meta: {
    needLogin: false
  }
},
{
  path: '/searchList',
  name: '搜索结果',
  component: () => import('_views/SearchList'),
  meta: {
    needLogin: false
  }
},
{
  path: '/order',
  name: '订单页',
  component: () => import('_views/Order'),
  meta: {
    needLogin: false
  }
},
{
  path: '/addressList',
  name: '收货人列表',
  component: () => import('_views/AddressList'),
  meta: {
    needLogin: true
  }
},
{
  path: '/addressAdd',
  name: '新增收货地址',
  component: () => import('_views/AddressAdd'),
  meta: {
    needLogin: true
  }
},
{
  path: '/addressUpdate',
  name: '修改联系人',
  component: () => import('_views/AddressUpdate'),
  meta: {
    needLogin: true
  }
}

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 路由前置守卫做登录鉴权
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    // 需要做登录
    if (isLogin()) {
      // 登录了
      next()
    } else {
      // 没有登录
      next({
        path: '/login',
        query: {
          from: to.path
        }
      })
    }
  } else {
    // 不需要登录
    next()
  }
})
export default router
